package org.millioncall.yueyoga.admin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Hall;
import org.millioncall.yueyoga.admin.service.HallService;
import org.millioncall.yueyoga.common.GlobalConstants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HallAction extends BaseAction {
	private HallService hallService;

	public HallService getHallService() {
		return hallService;
	}

	public void setHallService(HallService hallService) {
		this.hallService = hallService;
	}

	// 添加瑜伽会馆
	@RequestMapping(value = "/toAddHall.jhtml", method = RequestMethod.GET)
	public String toAddHall(HttpServletRequest request, ModelMap context) {
		super.initFrontHeader(request, context);

		List<Hall> halls = hallService.findHalls();
		context.put("halls", halls);
		context.put("assetsPath", GlobalConstants.ASSETS_PATH);
		return "hall_add";
	}

	// 添加瑜伽会馆
	@RequestMapping(value = "/addHall.jhtml", method = RequestMethod.POST)
	public String addHall(String name, String code, String address,
			String remarks, String phone, String assignedTo, ModelMap context,
			HttpServletRequest request, HttpServletResponse response) {

		super.initFrontHeader(request, context);

		String msg = null;
		Hall hall = new Hall();
		if (name == null || name.equals("")) {
			msg = "会馆名称不能为空！";
		} else {
			hall.setName(name);
		}
		if (code == null || code.equals("")) {
			msg = "会馆地址不能为空！";
		} else {
			hall.setCode(code);
		}
		if (address == null || address.equals("")) {
			msg = "会馆地址不能为空！";
		} else {
			hall.setAddress(address);
		}
		hall.setPhone(phone);
		hall.setRemarks(remarks);
		hall.setAssignedto(assignedTo);
		if (msg == null) {
			if (hallService.findByName(name) == null) {
				if (hallService.findByCode(code) == null) {
					if (hallService.saveHall(hall)) {
						context.clear();
						context.put("msg", "添加瑜伽会馆成功");
						return "redirect:toHallList.jhtml";
					} else {
						context.put("msg", "添加瑜伽会馆失败，请稍候重试或联系管理员");
						context.put("assetsPath", GlobalConstants.ASSETS_PATH);
						return "hall_add";
					}
				} else {
					context.put("msg", "会馆编号已存在！");
					context.put("assetsPath", GlobalConstants.ASSETS_PATH);
					return "hall_add";
				}
			} else {
				context.put("msg", "会馆名称已存在！");
				context.put("assetsPath", GlobalConstants.ASSETS_PATH);
				return "hall_add";
			}
		} else {
			context.put("msg", msg);
			context.put("assetsPath", GlobalConstants.ASSETS_PATH);
			return "hall_add";
		}
	}

	// 添加瑜伽会馆
	@RequestMapping(value = "/toEditHall.jhtml", method = RequestMethod.GET)
	public String toEditHall(ModelMap context, String code,
			HttpServletRequest request, HttpServletResponse response) {

		super.initFrontHeader(request, context);

		Hall hall = hallService.findByCode(code);
		context.put("hall", hall);
		return "hall_edit";
	}

	// 添加瑜伽会馆
	@RequestMapping(value = "/editHall.jhtml", method = RequestMethod.POST)
	public String editHall(String hallCode, String name, String code,
			String address, String remarks, String phone, String assignedTo,
			ModelMap context, HttpServletRequest request,
			HttpServletResponse response) {

		super.initFrontHeader(request, context);

		Hall hall = hallService.findByCode(hallCode);
		hall.setName(name);
		hall.setCode(code);
		hall.setAddress(address);
		hall.setPhone(phone);
		hall.setRemarks(remarks);
		hall.setAssignedto(assignedTo);
		if (hallService.updateHall(hall)) {
			context.clear();
			return "redirect:toHallList.jhtml";
		} else {
			return "hall_edit";
		}
	}

	// 查询所有瑜伽会馆
	@RequestMapping(value = "/findHall.jhtml")
	public void findHall(ModelMap context, HttpServletRequest request,
			HttpServletResponse response) {

		super.initFrontHeader(request, context);

		List<Hall> halls = hallService.findHalls();
		context.put("halls", halls);
	}

	// 查瑜伽会馆列表
	@RequestMapping(value = "/toHallList.jhtml")
	public String findHallList(ModelMap context, HttpServletRequest request,
			HttpServletResponse response) {
		super.initFrontHeader(request, context);

		int pageNo = 1;
		int pageSize = 20;

		String strStart = request.getParameter("pageNo");// 获得页码数
		if (strStart != null) {
			pageNo = Integer.parseInt(strStart);
		}
		String strLimit = request.getParameter("limit");
		if (strLimit != null) {
			pageSize = Integer.parseInt(strLimit);
		}
		PaginatedList<Hall> p = hallService.getStatusInPage((pageNo - 1) * 10,
				pageSize);
		context.put("hallList", p);
		return "hall_list";
	}

	// 删除会员
	@RequestMapping(value = "/deleteHall.jhtml")
	public void deleteHall(HttpServletRequest request,
			HttpServletResponse response) {

		String msg = null;
		String ids = request.getParameter("ids");
		if (StringUtils.isBlank(ids)) {
			msg = "{'success':false,'msg':'未提交待删除会馆的编号'}";
		} else {
			String[] arrids = ids.split(",");
			Integer[] deids = new Integer[20];
			for (int i = 0; i < arrids.length; i++) {
				if (arrids[i] != null && !arrids[i].equals("")) {
					deids[i] = Integer.parseInt(arrids[i]);
				}
			}
			try {
				hallService.deleteHallByIds(deids);
				msg = "true";
			} catch (RuntimeException re) {
				msg = "false";
			}
		}
		WebAppUtils.ajaxResponse(response, msg);
	}
}
